const { clone } = require("lodash")

// 神拷贝
function deepClone(obj, cache = new WeakMap()){
    if(typeof obj != 'object') return obj
    if(obj === null) return obj
    if(cache.get(obj)) return cache.get(obj) //防止循环引用，程序进入死循环
    if(obj instanceof Date) return new Date(obj)
    if(obj instanceof RegExp) return new RegExp(obj)
    let cloneObj = new obj.constructor()
    cache.set(obj, cloneObj)
    for(let key in obj){
        if(obj.hasOwnProperty(key)){
            cloneObj[key] = deepClone(obj[key], cache)
        }
    }
    return cloneObj
}

// 测试
const obj = {
    name: 'yanessa', 
    age: 18,
    birth: new Date('1999-01-01')
}

const newObj = deepClone(obj)
console.log(newObj)
console.log(obj === newObj)